//=====================================================//
//begin XML processor.
//global variables
var stores = new Array();
var storesUs = new Array();
var storesGlobal = new Array();
//get the stores from the site. 
//input: storeUrl: the file in server want to get. 
function getStores(storeUrl, parseXml) {
    $.ajax({
        url: storeUrl,
        dataType: "xml",
        success: parseXml
    });
}
//parse the xml file.
function parseXmlUs(xml) {
    $(xml).find('state').each(function () {
        var state = $(this);
        var store = new Object();
        store.Id = state.attr('id');
        store.Name = state.attr('name');
        store.Dealers = new Array();
        state.find('dealer').each(function () {
            var dealer = $(this);
            var d = new Object();
            d.Image = dealer.attr("image");
            d.Address = dealer.attr('adress');
            d.Area = dealer.attr('area');
            d.City = dealer.attr('city');
            d.Phone = dealer.attr('phone');
            d.Week = dealer.attr('week');
            d.Weekend = dealer.attr('weekend');
            d.Weekend2 = dealer.attr('weekend2');
            d.LinkDisplay = dealer.attr('linkdisplay');
            d.Website = dealer.attr('website');
            store.Dealers.push(d);
        });
        storesUs.push(store);
    });
};
function parseXmlGlobal(xml) {
    $(xml).find('state').each(function () {
        var state = $(this);
        var store = new Object();
        store.Id = state.attr('id');
        store.Name = state.attr('name');
        store.Dealers = new Array();
        state.find('dealer').each(function () {
            var dealer = $(this);
            var d = new Object();
            d.Image = dealer.attr("image");
            d.Address = dealer.attr('adress');
            d.Area = dealer.attr('area');
            d.City = dealer.attr('city');
            d.Phone = dealer.attr('phone');
            d.Week = dealer.attr('week');
            d.Weekend = dealer.attr('weekend');
            d.Weekend2 = dealer.attr('weekend2');
            d.LinkDisplay = dealer.attr('linkdisplay');
            d.Website = dealer.attr('website');
            store.Dealers.push(d);
        });
        storesGlobal.push(store);
    });
    stores = storesGlobal;

}
//get the store by id.
function getStoreById(storeId) {
    for (i = 0; i < stores.length; i++) {
        if (stores[i]["Id"] == storeId) {
            return stores[i];
        }
    };
};
function swapToStores(store) {
    if (store == 'us') {
        stores = storesUs;
    }
    else {
        stores = storesGlobal;
    }
}
//end XML Processor.

//Page processor
//=============================================================
var store_tooltip = "#store_tooltip";
var popupwindow = ".popupwindow";
var popupLayout = ".popupLayout";
var myStoreState = "#myStoreState";
var contentInfoBoxTitle = ".contentInfoBoxTitle";
var contentInfoBox = ".contentInfoBox";
var domain = "http://www.7forallmankind.com";

//offset to calculate relative position with while show tooltip


function showStore(e, text) {
    var offsetLeft = $('#map_wraper').offset().left;
    var offsetTop = $('#map_wraper').offset().top;
    $(myStoreState).text(text);

    $(store_tooltip).css({
        top: e.pageY - offsetTop - $(store_tooltip).outerHeight(true) - 10, left: e.pageX - offsetLeft - 45, opacity: 0.9
    });
    $(store_tooltip).show();
};

function moveStore(e) {
    var offsetLeft = $('#map_wraper').offset().left;
    var offsetTop = $('#map_wraper').offset().top;
    $(store_tooltip).css({
        top: e.pageY - offsetTop - $(store_tooltip).outerHeight(true) - 10, left: e.pageX - offsetLeft - 45
    });
};

function hideStore(e) {
    $(store_tooltip).hide();
}

function showDealers(storeId) {
    $(popupLayout).css({
        backgroundColor: '#F2F2F2',
        opacity: 0.4
    }).show();
    $(popupwindow).fadeIn(500);
    $(store_tooltip).hide();
    var store = getStoreById(storeId);
    var dealerContent = "<p class='contentInfoBoxTitle'>" + store["Name"] + "</p>";
    var dealers = store["Dealers"];
    for (var i = 0; i < dealers.length; i++) {
        dealerContent += buildDealer(dealers[i]);
    }
    $(contentInfoBoxTitle).text(store["Name"]);
    $(contentInfoBox).html('');
    $(contentInfoBox).append(dealerContent);
    $(contentInfoBox + " div.infoBox a").click(function (e) {
        e.stopPropagation();
    });
};

function load(html) {
    var load = window.open(unescape(html));
}

function buildDealer(dealer) {
    content = "<div class='infoBox' onclick=window.open('" + domain + "')>";
    content += "<img src='" + domain + dealer["Image"] + "' onerror='hideImage(this);' />";
    content += "<p class='p1'>" + dealer["Area"] + "<br /></p>";
    content += "<p>";
    content += dealer["Address"] + "<br />";
    if (dealer["City"] != '')
        content += dealer["City"] + "<br />";
    if (dealer["Phone"] != '')
        content += dealer["Phone"] + "<br />";
    if (dealer["Week"] != '')
        content += dealer["Week"] + "<br />";
    if (dealer["Weekend"] != '')
        content += dealer["Weekend"] + "<br />";
    if (dealer["Weekend2"] != '')
        content += dealer["Weekend2"] + "<br />";
    content += "</p>";
    if(dealer["LinkDisplay"] !='')
        content += "<p>" + dealer["LinkDisplay"] + "</p>";
    if (dealer["Website"] != '')
        content += "<p><a href='" + dealer["Website"] + "' target='_blank'><span>Driving Directions</span></a></p>";
    content += "<div class='clear'></div></div>";
    return content;
};
function hideImage(image) {
    $(image).hide();
};
function bindMouseEvents() {
    $("#map_wraper div.store a").click(function () {
        storeId = $(this).attr('name');
        showDealers(storeId);
    });
    $("#map_wraper div.store a").mouseover(function (e) {
        showStore(e, $(this).attr("state"));
    });
    $("#map_wraper div.store a").mousemove(function (e) {
        moveStore(e);
    });
    $("#map_wraper div.store a").mouseout(function (e) {
        hideStore(e);
    });
};

function initMap() {
    bindMouseEvents();
    mouse_is_inside = false;
    $(popupwindow).hover(function () {
        mouse_is_inside = true;
    }, function () {
        mouse_is_inside = false;
    });
    $("body").mouseup(function () {
        if (!mouse_is_inside) {
            $(popupLayout).hide();
            $(popupwindow).hide();
        }
    });

    $('#close_popup').click(function () {
        $(popupLayout).hide();
        $(popupwindow).hide();
    });
    getStores('stores_international.xml', parseXmlGlobal);
    getStores('stores_domestic.xml', parseXmlUs);
    $('#return_global_map').click(function () {
        swapToStores('global');
        $('#us_map').hide();
        $('#global_map').show();
    });

    $('.us_store').click(function () {
        swapToStores('us');
        $('#global_map').hide();
        $('#us_map').show();
    });
};
